﻿using Abp;
using Abp.Application.Services;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.Com.Dto;
using FwProject.Manager;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using Abp.Authorization;
namespace FwProject.Com
{
    /// <summary>
    /// 收货地址管理
    /// </summary>
    [AbpAuthorize]
    public  class ShopReceivingAddressAppService : FwProjectAppServiceBase, IShopReceivingAddressAppService
    {
        private ShopReceivingAddressManager _shopReceivingAddressManager;
        private readonly IRepository<ShopReceivingAddress, Guid> _repositoryShopReceivingAddress;
        /// <summary>
        /// 主键
        /// </summary>
        public IGuidGenerator _guid { get; set; }
        /// <summary>
        /// 映射对象
        /// </summary>
        public IObjectMapper _map { get; set; }
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="shopReceivingAddressManager"></param>
        /// <param name="repositoryShopReceivingAddress"></param>
        public ShopReceivingAddressAppService(ShopReceivingAddressManager shopReceivingAddressManager,
            IRepository<ShopReceivingAddress, Guid> repositoryShopReceivingAddress) {
            _shopReceivingAddressManager = shopReceivingAddressManager;
            _repositoryShopReceivingAddress = repositoryShopReceivingAddress;
            _guid = SequentialGuidGenerator.Instance;
        }
        /// <summary>
        /// 创建一个收货地址
        /// </summary>
        /// <param name="model">创建模型</param>
        /// <returns></returns>
        public async Task<Guid> CreateShopReceivingAddress(ShopReceivingAddressInput model)
        {
            var m = _map.Map<ShopReceivingAddress>(model);
            m.Id = _guid.Create();
            m.UserId = AbpSession.UserId.Value;
            return await _shopReceivingAddressManager.CreateShopReceivingAddress(m);
        }
        /// <summary>
        /// 删除收货地址
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task CleanRemoveShopReceivingAddress(Guid id)
        {
            var model= _repositoryShopReceivingAddress.Get(id);
            await _shopReceivingAddressManager.DeleteShopReceivingAddress(model);
        }
        /// <summary>
        /// 设为默认地址
        /// </summary>
        /// <param name="Id">主键</param>
        public void SetDefalutAddress(Guid Id)
        {
            _shopReceivingAddressManager.SetDefalutAddress(Id);
        }
        /// <summary>
        /// 修改收货地址
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="model">模型数据</param>
        /// <returns></returns>
        public async Task ModifyShopReceivingAddress(ShopReceivingAddressEdit model)
        {
             var m = _repositoryShopReceivingAddress.Get(model.Id);
             m = _map.Map(model, m);
            await _shopReceivingAddressManager.UpdateShopReceivingAddress(m);
        }
        /// <summary>
        /// 获取用户所有的收货地址
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="pageSize">条数</param>
        /// <returns></returns>
        public async Task<IPagedList<ShopReceivingAddressDto>> GetShopReceivingAddressByList(int page,int pageSize)
        {
            return await _repositoryShopReceivingAddress.GetAll().Where(o => o.UserId == AbpSession.UserId.Value).OrderByDescending(o => o.DefalutAddress).ProjectTo<ShopReceivingAddressDto>().ToPagedListAsync(page, pageSize);

        }

        /// <summary>
        /// 获取默认收货地址
        /// </summary>
        /// <returns></returns>
        public ShopReceivingAddressDto GetShopReceivingAddressByIsDefalut()
        {
            var model=  _repositoryShopReceivingAddress.GetAll().Where(o => o.UserId == AbpSession.UserId.Value && o.DefalutAddress==true).ProjectTo<ShopReceivingAddressDto>().FirstOrDefault();
            return model;
        }

    }
}
